﻿using AForge.Video.DirectShow;
using AForge.Video;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection.Emit;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
using AForge.Fuzzy;

namespace CameraUSB
{
    public partial class Form1 : Form
    {
        private FilterInfoCollection videoDevices;
        private VideoCaptureDevice videoSource;
        private static int jj;
        string SaveFile = AppDomain.CurrentDomain.BaseDirectory + "\\Camera.xml";
        public Form1()
        {
            videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
            InitializeComponent();
            tbar_liangdu.SetRange(0, 255);
            tbar_baipingheng.SetRange(2800, 6500);
            tbar_baoguang.SetRange(-10, -2);
            tbar_baohedu.SetRange(0, 100);
            tbar_ruidu.SetRange(0, 7);
            tbar_beiguangbuchang.SetRange(0, 2);
            tbar_gama.SetRange(90, 150);
            tbar_sediao.SetRange(-180, 180);
            Data data = new Data();
            data = (Data)Serializition.LoadFromFile(typeof(Data), SaveFile);
            tbar_liangdu.Value = data.liangdu;
            tbar_baipingheng.Value = data.baipingheng;
            tbar_baoguang.Value = data.baoguang;
            tbar_baohedu.Value = data.baohedu;
            tbar_ruidu.Value = data.ruidu;
            tbar_beiguangbuchang.Value = data.beiguangbuchang;
            tbar_gama.Value = data.gama;
            tbar_sediao.Value = data.sediao;

            lab_liangdu.Text = data.liangdu.ToString();
            lab_baipingheng.Text = data.baipingheng.ToString(); ;
            lab_baoguang.Text = data.baoguang.ToString();
            lab_baohedu.Text = data.baohedu.ToString();
            lab_ruidu.Text = data.ruidu.ToString();
            lab_beiguangbuchang.Text = data.beiguangbuchang.ToString();
            lab_gama.Text = data.gama.ToString();
            lab_sediao.Text = data.sediao.ToString();
            //trackBar1_Scroll(null,null);
            //trackBar2_Scroll(null, null);
            //trackBar3_Scroll(null, null);
            //trackBar4_Scroll(null, null);
            //trackBar5_Scroll(null, null);
            //trackBar6_Scroll(null, null);
            //tbar_sediao_Scroll(null, null);
            //tbar_gama_Scroll(null, null);
            //trackBar1_Scroll(null, null);
            if (videoDevices.Count == 0)
            {
                throw new ApplicationException();
            }
            foreach (FilterInfo device in videoDevices)
            {
                comboBox1.Items.Add(device.Name);
            }
            comboBox1.SelectedIndex = 0;

        }

        private void trackBar1_Scroll(object sender, EventArgs e)
        {
            lab_liangdu.Text = tbar_liangdu.Value.ToString();
            if (videoSource != null)
                if (videoSource.IsRunning)
                {
                    videoSource.SetVideoProperty(
                               VideoProcAmpProperty.Brightness,
                        tbar_liangdu.Value,
                    VideoProcAmpFlags.Manual);
                }
        }

        private void trackBar2_Scroll(object sender, EventArgs e)
        {
            lab_baipingheng.Text = tbar_baipingheng.Value.ToString();
            if (videoSource != null)
                if (videoSource.IsRunning)
                {
                    videoSource.SetVideoProperty(
                               VideoProcAmpProperty.WhiteBalance,
                        tbar_baipingheng.Value,
                    VideoProcAmpFlags.Manual);
                }
        }

        private void trackBar3_Scroll(object sender, EventArgs e)
        {
            lab_baoguang.Text = tbar_baoguang.Value.ToString();
            if (videoSource != null)
                if (videoSource.IsRunning)
                {
                    videoSource.SetVideoProperty(
                              VideoProcAmpProperty.Contrast,
                         tbar_baoguang.Value,
                          VideoProcAmpFlags.Manual);
                }
        }

        private void trackBar4_Scroll(object sender, EventArgs e)
        {
            lab_baohedu.Text = tbar_baohedu.Value.ToString();
            if (videoSource != null)
                if (videoSource.IsRunning)
                {
                    videoSource.SetVideoProperty(
                               VideoProcAmpProperty.Saturation,
                         tbar_baohedu.Value,
                          VideoProcAmpFlags.Manual);
                }
        }

        private void trackBar5_Scroll(object sender, EventArgs e)
        {
            lab_ruidu.Text = tbar_ruidu.Value.ToString();
            if (videoSource != null)
                if (videoSource.IsRunning)
                {
                    videoSource.SetVideoProperty(
                               VideoProcAmpProperty.Sharpness,
                         tbar_ruidu.Value,
                          VideoProcAmpFlags.Manual);
                }
        }

        private void trackBar6_Scroll(object sender, EventArgs e)
        {
            lab_beiguangbuchang.Text = tbar_beiguangbuchang.Value.ToString();
            if (videoSource != null)
                if (videoSource.IsRunning)
                {
                    videoSource.SetVideoProperty(
                               VideoProcAmpProperty.BacklightCompensation,
                         tbar_beiguangbuchang.Value,
                          VideoProcAmpFlags.Manual);
                }
        }
        private void tbar_sediao_Scroll(object sender, EventArgs e)
        {
            lab_sediao.Text = tbar_sediao.Value.ToString();
            if (videoSource != null)
                if (videoSource.IsRunning)
                {
                    videoSource.SetVideoProperty(
                               VideoProcAmpProperty.Hue,
                          tbar_sediao.Value,
                          VideoProcAmpFlags.Manual);
                }
        }
        private void tbar_gama_Scroll(object sender, EventArgs e)
        {
            lab_gama.Text = tbar_gama.Value.ToString();
            if (videoSource != null)
                if (videoSource.IsRunning)
                {
                    videoSource.SetVideoProperty(
                               VideoProcAmpProperty.Gamma,
                          tbar_gama.Value,
                          VideoProcAmpFlags.Manual);
                }
        }


        private void button1_Click(object sender, EventArgs e)
        {
            CameraConn();
            trackBar1_Scroll(null, null);
            trackBar2_Scroll(null, null);
            trackBar3_Scroll(null, null);
            trackBar4_Scroll(null, null);
            trackBar5_Scroll(null, null);
            trackBar6_Scroll(null, null);
            tbar_sediao_Scroll(null, null);
            tbar_gama_Scroll(null, null);
            trackBar1_Scroll(null, null);
        }
        private void CameraConn()
        {
            videoSource = new VideoCaptureDevice(videoDevices[comboBox1.SelectedIndex].MonikerString);

            videoSourcePlayer1.VideoSource = videoSource;
            videoSourcePlayer1.Start();

        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (videoSource == null)
            {
                return;
            }
            videoSource.DisplayPropertyPage(IntPtr.Zero);
        }
        private void button3_Click(object sender, EventArgs e)
        {
            if (videoSource == null)
            {
                return;
            }
            if (videoSource.IsRunning)
            {
                VideoProcAmpFlags videoProcAmpFlags;
                videoSource.GetVideoProperty(
                           VideoProcAmpProperty.Brightness,
                   out int Liangdu,
                  out videoProcAmpFlags);
                tbar_liangdu.Value = Liangdu;
                lab_liangdu.Text = Liangdu.ToString();
            }
            if (videoSource.IsRunning)
            {
                VideoProcAmpFlags videoProcAmpFlags;
                videoSource.GetVideoProperty(
                           VideoProcAmpProperty.WhiteBalance,
                   out int Baipingheng,
                  out videoProcAmpFlags);

                tbar_baipingheng.Value = Baipingheng;
                lab_baipingheng.Text = Baipingheng.ToString();
            }
            //if (videoSource.IsRunning)
            //{
            //    VideoProcAmpFlags videoProcAmpFlags;
            //    videoSource.GetVideoProperty(
            //               VideoProcAmpProperty.Contrast,
            //       out int Baoguang,
            //      out videoProcAmpFlags);
            //    tbar_baoguang.Value = Baoguang;
            //    lab_baoguang.Text = Baoguang.ToString();
            //}
            if (videoSource.IsRunning)
            {
                VideoProcAmpFlags videoProcAmpFlags;
                videoSource.GetVideoProperty(
                           VideoProcAmpProperty.Saturation,
                   out int Baohedu,
                  out videoProcAmpFlags);
                tbar_baohedu.Value = Baohedu;
                lab_baohedu.Text = Baohedu.ToString();
            }
            if (videoSource.IsRunning)
            {
                VideoProcAmpFlags videoProcAmpFlags;
                videoSource.GetVideoProperty(
                           VideoProcAmpProperty.Sharpness,
                   out int Ruidu,
                  out videoProcAmpFlags);
                tbar_ruidu.Value = Ruidu;
                lab_ruidu.Text = Ruidu.ToString();
            }
            if (videoSource.IsRunning)
            {
                VideoProcAmpFlags videoProcAmpFlags;
                videoSource.GetVideoProperty(
                           VideoProcAmpProperty.BacklightCompensation,
                   out int Beiguangbuchang,
                  out videoProcAmpFlags);
                tbar_beiguangbuchang.Value = Beiguangbuchang;
                lab_beiguangbuchang.Text = Beiguangbuchang.ToString();
            }
            if (videoSource.IsRunning)
            {
                VideoProcAmpFlags videoProcAmpFlags;
                videoSource.GetVideoProperty(
                           VideoProcAmpProperty.Hue,
                   out int Sediao,
                  out videoProcAmpFlags);
                tbar_sediao.Value = Sediao;
                lab_sediao.Text = Sediao.ToString();
            }
            if (videoSource.IsRunning)
            {
                VideoProcAmpFlags videoProcAmpFlags;
                videoSource.GetVideoProperty(
                           VideoProcAmpProperty.Gamma,
                   out int Gama,
                  out videoProcAmpFlags);
                tbar_gama.Value = Gama;
                lab_gama.Text = Gama.ToString();
            }

        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (videoSource != null)
            {
                videoSource.Stop();
            }
            Data data = new Data()
            {
                liangdu = tbar_liangdu.Value,
                baipingheng = tbar_baipingheng.Value,
                baoguang = tbar_baoguang.Value,
                baohedu = tbar_baohedu.Value,
                ruidu = tbar_ruidu.Value,
                beiguangbuchang = tbar_beiguangbuchang.Value,
                gama = tbar_gama.Value,
                sediao = tbar_sediao.Value
            };
            Serializition.SaveToFile(data, SaveFile);
        }
    }
}
